import GameConfig from "../../Script/GameConfig";
import PaoMaMsgs, { EVENT_KEY_PAOMA_MESSAGE_ADD } from "./PaoMaMsg";


const { ccclass, property } = cc._decorator;


/**
 * 跑马灯对象
 */
@ccclass
export default class Paoma extends cc.Component {

	// 富文本节点
	@property(cc.RichText)
	private richText: cc.RichText = null;

	// 背景类型
	@property([cc.SpriteFrame])
	private bgType: cc.SpriteFrame[] = [];
	// 背景图片
	@property(cc.Sprite)
	private bgIcon: cc.Sprite = null;

	@property(cc.Button)
	private btnNode: cc.Button = null;

	// 开关按钮组件
	private set btnEnable(b: boolean) {
		const btn = this.btnNode;
		if (btn) {
			btn.interactable = b;
			btn.enabled = b;
		}
	}

	protected start() {
		// 输出序列中的消息
		this.showMsg();
	}

	protected onLoad() {
		this.actice = false;
		this.node.active = true;
		// 单款游戏，移除跳转游戏功能
		if (this.btnNode)
			this.btnNode.interactable = false;

		yy.event.on(this, yy.event.CustomEvent, this.onYYEvent);
	}
	protected onDestroy(): void {
		yy.event.off(yy.event.CustomEvent, this.onYYEvent);
	}
	private onYYEvent(e: yy.event.CustomEvent) {
		if (!!e && e.eventID == EVENT_KEY_PAOMA_MESSAGE_ADD) {
			this.showMsg();
		}
	}

	/**
	 * 更新跑马灯背景色
	 *
	 * @static
	 * @param {number} type 背景色序号
	 * @memberof Paoma
	 */
	public updateBg(type: number) {
		if (!!this.bgType) {
			// fixed the type
			type = Math.min(this.bgType.length - 1, Math.max(0, type));
			// change the bg spriteframe
			if (!!this.bgIcon)
				this.bgIcon.spriteFrame = this.bgType[type];
		}
		this._typeIdx = type;
	}
	/**
	 * 记录一份数据，对象初始化时会读取该值
	 */
	private _typeIdx: number = 0;


	// check and show animation
	private showMsg() {
		if (!PaoMaMsgs.msgAry || PaoMaMsgs.msgAry.length == 0 || this.isAnimating) return;
		this.isAnimating = true;
		this.richText.string = PaoMaMsgs.msgAry[0].msg;
		this.scheduleOnce(this.playAnimation.bind(this), 0.1);
	}
	// play animation
	private isAnimating: boolean = false;
	private playAnimation() {
		const that = this;
		this.richText.node.stopAllActions();
		this.richText.node.runAction(cc.sequence(
			cc.callFunc(() => {
				// init
				that.richText.node.x = that.boxRect.xMax + 10;
				that.actice = true;
			}),
			cc.moveTo(that.moveSeconds, -that.moveDistance, that.msgYpos),
			cc.callFunc(() => {
				// end
				that.richText.node.x = that.boxRect.xMax + 10;
				PaoMaMsgs.msgAry.shift();
				that.isAnimating = false;
				if (PaoMaMsgs.msgAry.length > 0) {
					that.showMsg();
				} else {
					that.actice = false;
				}
			})
		));
	}
	// 跑马灯包围盒区域
	private get boxRect(): cc.Rect {
		return this.node.getBoundingBox();
	}
	// 消息长度
	private get msgWidth(): number {
		return this.richText.node.width;
	}
	// 消息Y位置
	private get msgYpos(): number {
		return this.richText.node.y;
	}
	// 跑马灯移动距离
	private get moveDistance(): number {
		// 两侧多移动10px
		const disTance = this.msgWidth + this.boxRect.width + 10 * 2;
		return disTance;
	}
	// 计算动画时长（恒定速率）
	private get moveSeconds(): number {
		const speed = 100;	// 100 px/s
		return this.moveDistance / speed;
	}

	// show the node;暂无关闭开启动画
	private set actice(v: boolean) {
		if (this.bgIcon)
			this.bgIcon.node.active = v;
		this.richText.node.opacity = v ? 255 : 0;
		this.btnEnable = v && this.canGo;
	}

	// 是否能够切换游戏
	private get canGo(): boolean {
		return PaoMaMsgs.msgAry[0]
			&& PaoMaMsgs.msgAry[0].gameID > 0
			&& PaoMaMsgs.msgAry[0].gameID != (yy.room.getGameID() || GameConfig.GameID);
	}

}
